package de.softwareforge.published.doclet;

/*
 * Copyright (C) 2007-2008 Henning P. Schmiedehausen
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import com.sun.javadoc.DocErrorReporter;
import com.sun.tools.doclets.standard.Standard;

import de.softwareforge.published.util.Locale;
import de.softwareforge.published.util.PublishInformation;
import de.softwareforge.published.util.Utils;


/**
 * The configuration for the {@link PublishedDoclet} doclet.
 *
 * @author <a href="mailto:henning@schmiedehausen.org">Henning P. Schmiedehausen</a>
 * @version $Id: PublishedDocletConfiguration.java 96 2008-02-24 11:24:16Z hps@intermeta.de $
 * @api.level unpublished
 */
public class PublishedDocletConfiguration
{

    /** The '-publishlevel' command. */
    public static final String CMD_PUBLISHLEVEL = "-publishlevel";

    /** The '-defaultpublishlevel' command. */
    public static final String CMD_DEFAULTPUBLISHLEVEL = "-defaultpublishlevel";

    /** The '-rootpublishlevel' command. */
    public static final String CMD_ROOTPUBLISHLEVEL = "-rootpublishlevel";

    /** The '-hideuntagged' command. */
    public static final String CMD_HIDEUNTAGGED = "-hideuntagged";

    /** The '-novisibilitychanges' command. */
    public static final String CMD_NOVISIBILITYCHANGES = "-novisibilitychanges";

    /** The '-ignoretags' command. */
    public static final String CMD_IGNORETAGS = "-ignoretags";

    /** The '-publishall' command. */
    public static final String CMD_PUBLISHALL = "-publishall";

    private PublishInformation publishLevel = PublishInformation.UNPUBLISHED;

    private PublishInformation defaultPublishLevel = null;

    private PublishInformation rootPublishLevel = null;

    private boolean hideUntagged = false;

    private boolean noVisibilityChanges = false;

    private boolean ignoreTags = false;

    private boolean publishAll = false;

    /**
     * Parse the given options.
     *
     * @param options Array of options and their parameters.
     * @param reporter A {@link DocErrorReporter} object to print out messages, warnings and errors.
     *
     * @return True if all options were parsed successfully.
     */
    protected boolean validOptions(final String[][] options, final DocErrorReporter reporter)
    {
        Locale messages = new Locale(PublishedDoclet.LOCALE_RESOURCES, reporter);

        if (!Standard.validOptions(options, reporter))
        {
            return false;
        }

        for (int i = 0; i < options.length; i++)
        {
            String[] optinfo = options[i];

            if (CMD_PUBLISHLEVEL.equalsIgnoreCase(optinfo[0]))
            {

                // Javadoc makes sure that optinfo[1] always exists!
                String[] fields = Utils.parseOption(optinfo[1], "command line", messages);

                if (fields == null)
                {
                    return false;
                }

                publishLevel = Utils.getPublishInformation(fields, "command line", messages);

                if (publishLevel == null)
                {
                    return false;
                }
            }
            else if (CMD_DEFAULTPUBLISHLEVEL.equalsIgnoreCase(optinfo[0]))
            {

                // Javadoc makes sure that optinfo[1] always exists!
                String[] fields = Utils.parseOption(optinfo[1], "command line", messages);

                if (fields == null)
                {
                    return false;
                }

                defaultPublishLevel = Utils.getPublishInformation(fields, "command line", messages);

                if (publishLevel == null)
                {
                    return false;
                }
            }
            else if (CMD_ROOTPUBLISHLEVEL.equalsIgnoreCase(optinfo[0]))
            {

                // Javadoc makes sure that optinfo[1] always exists!
                String[] fields = Utils.parseOption(optinfo[1], "command line", messages);

                if (fields == null)
                {
                    return false;
                }

                rootPublishLevel = Utils.getPublishInformation(fields, "command line", messages);

                if (publishLevel == null)
                {
                    return false;
                }
            }
            else if (CMD_HIDEUNTAGGED.equalsIgnoreCase(optinfo[0]))
            {
                hideUntagged = true;
            }
            else if (CMD_NOVISIBILITYCHANGES.equalsIgnoreCase(optinfo[0]))
            {
                noVisibilityChanges = true;
            }
            else if (CMD_IGNORETAGS.equalsIgnoreCase(optinfo[0]))
            {
                ignoreTags = true;
            }
            else if (CMD_PUBLISHALL.equalsIgnoreCase(optinfo[0]))
            {
                publishAll = true;
            }
        }

        return true;
    }

    /**
     * Return the appropriate option length for a given argument, if known.
     *
     * @param option The option to check.
     *
     * @return The number of arguments required for this option or 0 if the option is unknown.
     */
    protected int optionLength(final String option)
    {
        int result = -1;

        if ((result = Standard.optionLength(option)) > 0)
        {
            return result;
        }

        if (CMD_PUBLISHLEVEL.equalsIgnoreCase(option))
        {
            return 2;
        }

        if (CMD_DEFAULTPUBLISHLEVEL.equalsIgnoreCase(option))
        {
            return 2;
        }

        if (CMD_ROOTPUBLISHLEVEL.equalsIgnoreCase(option))
        {
            return 2;
        }

        if (CMD_HIDEUNTAGGED.equalsIgnoreCase(option))
        {
            return 1;
        }

        if (CMD_NOVISIBILITYCHANGES.equalsIgnoreCase(option))
        {
            return 1;
        }

        if (CMD_IGNORETAGS.equalsIgnoreCase(option))
        {
            return 1;
        }

        if (CMD_PUBLISHALL.equalsIgnoreCase(option))
        {
            return 1;
        }

        return 0;
    }

    /**
     * Returns the default {@link PublishInformation} object.
     *
     * @return The default {@link PublishInformation} object. Can be null, in that case no default information is assigned.
     */
    public PublishInformation getDefaultPublishLevel()
    {
        return defaultPublishLevel;
    }

    /**
     * Returns the {@link PublishInformation} object for the root element.
     *
     * @return The {@link PublishInformation} object which represents the root element. Can be null, in that case no default
     *         information is assigned.
     */
    public PublishInformation getRootPublishLevel()
    {
        return rootPublishLevel;
    }

    /**
     * Returns the minimum {@link PublishInformation} object.
     *
     * @return The minimum {@link PublishInformation} object.
     */
    public PublishInformation getPublishLevel()
    {
        return publishLevel;
    }

    /**
     * Should elements without publishing information be hidden?
     *
     * @return True if elements without publishing information should be hidden.
     */
    public boolean isHideUntagged()
    {
        return hideUntagged;
    }

    /**
     * Returns True if any visibility changes should stop the Javadoc rendering.
     *
     * @return True if any visibility change should stop the Javadoc rendering.
     */
    public boolean isNoVisibilityChanges()
    {
        return noVisibilityChanges;
    }

    /**
     * Returns True if all API level tags should be ignored.
     *
     * @return True if all API level tags should be ignored.
     */
    public boolean isIgnoreTags()
    {
        return ignoreTags;
    }

    /**
     * Returns true if all API level tags should be published.
     *
     * @return True if all API level tags should be published.
     */
    public boolean isPublishAll()
    {
        return publishAll;
    }

    /**
     * @see Object#toString()
     */
    public String toString()
    {
        return "/default publish level: " + ((getDefaultPublishLevel() == null) ? "<unset>" : getDefaultPublishLevel())
                + " /ignore tags: " + isIgnoreTags()
                + " /root publish level: " + ((getRootPublishLevel() == null) ? "<unset>" : getRootPublishLevel())
                + " /hide untagged: " + isHideUntagged()
                + " /publish all: " + isPublishAll()
                + " /publish level: " + ((getPublishLevel() == null) ? "<unset>" : getPublishLevel())
                + " /no visibility changes: " + isNoVisibilityChanges();
    }
}
